//
//  TF_MoreConfigurationButton.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/18.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_MoreConfigurationButton: UIButton {
    @objc public var modelType = ""
    @objc public var number:NSInteger = 0
    @objc public let numberLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds = false
        self.numberLabel.textAlignment = .center
        self.numberLabel.textColor = .white
        self.numberLabel.layer.masksToBounds = true
        self.numberLabel.backgroundColor = .red
        self.numberLabel.font = UIFont.systemFont(ofSize: 12)
        self.addSubview(self.numberLabel)
        
    }

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        self.imageView!.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 50)
        
        
        self.titleLabel!.frame = CGRect(x: -10, y: 57, width: self.frame.size.width + 20, height: 20)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func drawRedDotWidthNumber(_ number:NSInteger)
    {
       let myString =  String.init(format: "%i", number)
        
        let redViewSize:CGSize = myString.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)])
        
        let testMsg:String = "12"
        
        let tempSize:CGSize = testMsg.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)])
        
      

        var redViewFrameWidth:CGFloat = 0;
        if(redViewSize.width >= tempSize.width)
        {
            redViewFrameWidth = redViewSize.width + 8.0;
        }else
        {
            redViewFrameWidth = tempSize.width + 8.0;
        }
        let redViewFrameHeight = redViewFrameWidth;
        
     
        self.numberLabel.layer.cornerRadius = redViewFrameHeight / 2;
        self.numberLabel.bounds = CGRect(x: 0, y: 0, width: redViewFrameWidth, height: redViewFrameHeight)
        
        self.numberLabel.center = CGPoint(x: self.frame.size.width-(redViewFrameWidth/2), y: 10)
       
        if (number != 0) {
             self.numberLabel.isHidden = false
             self.numberLabel.text = myString
        }else{
            self.numberLabel.isHidden = true
        }
       

        
    }
    //清除小红点
    func clearRedDot()
    {
        self.number = 0;
        self.numberLabel.isHidden = true
    }
}
